package o;

import android.graphics.Color;
import android.util.TimingLogger;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.ActivityChooserView.ActivityChooserViewAdapter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import o.lz.b;

final class ma {
    private static final Comparator<e> ᐝ = new Comparator<e>() {
        public /* synthetic */ int compare(Object obj, Object obj2) {
            return ˏ((e) obj, (e) obj2);
        }

        public int ˏ(e eVar, e eVar2) {
            return eVar2.ॱ() - eVar.ॱ();
        }
    };
    private final float[] ʽ = new float[3];
    final int[] ˊ;
    @Nullable
    final TimingLogger ˋ = null;
    final int[] ˎ;
    final o.lz.e[] ˏ;
    final List<b> ॱ;

    class e {
        private int ʻ;
        private int ʼ;
        private int ʽ;
        final /* synthetic */ ma ˊ;
        private int ˋ;
        private int ˎ;
        private int ˏ;
        private int ॱ;
        private int ॱॱ;
        private int ᐝ;

        e(ma maVar, int i, int i2) {
            this.ˊ = maVar;
            this.ॱ = i;
            this.ˏ = i2;
            ˏ();
        }

        final int ॱ() {
            return (((this.ʼ - this.ˎ) + 1) * ((this.ʽ - this.ᐝ) + 1)) * ((this.ʻ - this.ॱॱ) + 1);
        }

        final boolean ˋ() {
            return ˎ() > 1;
        }

        final int ˎ() {
            return (this.ˏ + 1) - this.ॱ;
        }

        final void ˏ() {
            int[] iArr = this.ˊ.ˎ;
            int[] iArr2 = this.ˊ.ˊ;
            int i = Integer.MIN_VALUE;
            int i2 = 0;
            int i3 = Integer.MIN_VALUE;
            int i4 = ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED;
            int i5 = ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED;
            int i6 = ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED;
            int i7 = Integer.MIN_VALUE;
            for (int i8 = this.ॱ; i8 <= this.ˏ; i8++) {
                int i9 = iArr[i8];
                i2 += iArr2[i9];
                int ˏ = ma.ˏ(i9);
                int ˊ = ma.ˊ(i9);
                i9 = ma.ˋ(i9);
                if (ˏ > i3) {
                    i3 = ˏ;
                }
                if (ˏ < i6) {
                    i6 = ˏ;
                }
                if (ˊ > i7) {
                    i7 = ˊ;
                }
                if (ˊ < i5) {
                    i5 = ˊ;
                }
                if (i9 > i) {
                    i = i9;
                }
                if (i9 < i4) {
                    i4 = i9;
                }
            }
            this.ˎ = i6;
            this.ʼ = i3;
            this.ᐝ = i5;
            this.ʽ = i7;
            this.ॱॱ = i4;
            this.ʻ = i;
            this.ˋ = i2;
        }

        final e ˊ() {
            if (ˋ()) {
                int ʽ = ʽ();
                e eVar = new e(this.ˊ, ʽ + 1, this.ˏ);
                this.ˏ = ʽ;
                ˏ();
                return eVar;
            }
            throw new IllegalStateException("Can not split a box with only 1 color");
        }

        final int ʻ() {
            int i = this.ʼ - this.ˎ;
            int i2 = this.ʽ - this.ᐝ;
            int i3 = this.ʻ - this.ॱॱ;
            if (i >= i2 && i >= i3) {
                return -3;
            }
            if (i2 < i || i2 < i3) {
                return -1;
            }
            return -2;
        }

        final int ʽ() {
            int ʻ = ʻ();
            int[] iArr = this.ˊ.ˎ;
            int[] iArr2 = this.ˊ.ˊ;
            ma.ˋ(iArr, ʻ, this.ॱ, this.ˏ);
            Arrays.sort(iArr, this.ॱ, this.ˏ + 1);
            ma.ˋ(iArr, ʻ, this.ॱ, this.ˏ);
            int i = this.ˋ / 2;
            ʻ = 0;
            for (int i2 = this.ॱ; i2 <= this.ˏ; i2++) {
                ʻ += iArr2[iArr[i2]];
                if (ʻ >= i) {
                    return Math.min(this.ˏ - 1, i2);
                }
            }
            return this.ॱ;
        }

        final b ʼ() {
            int i = 0;
            int[] iArr = this.ˊ.ˎ;
            int[] iArr2 = this.ˊ.ˊ;
            int i2 = 0;
            int i3 = 0;
            int i4 = 0;
            for (int i5 = this.ॱ; i5 <= this.ˏ; i5++) {
                int i6 = iArr[i5];
                int i7 = iArr2[i6];
                i += i7;
                i4 += ma.ˏ(i6) * i7;
                i3 += ma.ˊ(i6) * i7;
                i2 += ma.ˋ(i6) * i7;
            }
            return new b(ma.ˋ(Math.round(((float) i4) / ((float) i)), Math.round(((float) i3) / ((float) i)), Math.round(((float) i2) / ((float) i))), i);
        }
    }

    ma(int[] iArr, int i, o.lz.e[] eVarArr) {
        int i2;
        int ʽ;
        int i3 = 0;
        this.ˏ = eVarArr;
        int[] iArr2 = new int[32768];
        this.ˊ = iArr2;
        for (i2 = 0; i2 < iArr.length; i2++) {
            ʽ = ʽ(iArr[i2]);
            iArr[i2] = ʽ;
            iArr2[ʽ] = iArr2[ʽ] + 1;
        }
        i2 = 0;
        ʽ = 0;
        while (i2 < iArr2.length) {
            if (iArr2[i2] > 0 && ॱ(i2)) {
                iArr2[i2] = 0;
            }
            if (iArr2[i2] > 0) {
                ʽ++;
            }
            i2++;
        }
        int[] iArr3 = new int[ʽ];
        this.ˎ = iArr3;
        int i4 = 0;
        for (i2 = 0; i2 < iArr2.length; i2++) {
            if (iArr2[i2] > 0) {
                int i5 = i4 + 1;
                iArr3[i4] = i2;
                i4 = i5;
            }
        }
        if (ʽ <= i) {
            this.ॱ = new ArrayList();
            i2 = iArr3.length;
            while (i3 < i2) {
                ʽ = iArr3[i3];
                this.ॱ.add(new b(ॱॱ(ʽ), iArr2[ʽ]));
                i3++;
            }
            return;
        }
        this.ॱ = ˎ(i);
    }

    List<b> ˊ() {
        return this.ॱ;
    }

    private List<b> ˎ(int i) {
        Collection priorityQueue = new PriorityQueue(i, ᐝ);
        priorityQueue.offer(new e(this, 0, this.ˎ.length - 1));
        ˋ(priorityQueue, i);
        return ˊ(priorityQueue);
    }

    private void ˋ(PriorityQueue<e> priorityQueue, int i) {
        while (priorityQueue.size() < i) {
            e eVar = (e) priorityQueue.poll();
            if (eVar != null && eVar.ˋ()) {
                priorityQueue.offer(eVar.ˊ());
                priorityQueue.offer(eVar);
            } else {
                return;
            }
        }
    }

    private List<b> ˊ(Collection<e> collection) {
        List arrayList = new ArrayList(collection.size());
        for (e ʼ : collection) {
            b ʼ2 = ʼ.ʼ();
            if (!ˋ(ʼ2)) {
                arrayList.add(ʼ2);
            }
        }
        return arrayList;
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    static void ˋ(int[] r3, int r4, int r5, int r6) {
        /*
        switch(r4) {
            case -3: goto L_0x0003;
            case -2: goto L_0x0004;
            case -1: goto L_0x001f;
            default: goto L_0x0003;
        };
    L_0x0003:
        return;
    L_0x0004:
        if (r5 > r6) goto L_0x0003;
    L_0x0006:
        r0 = r3[r5];
        r1 = ˊ(r0);
        r1 = r1 << 10;
        r2 = ˏ(r0);
        r2 = r2 << 5;
        r1 = r1 | r2;
        r0 = ˋ(r0);
        r0 = r0 | r1;
        r3[r5] = r0;
        r5 = r5 + 1;
        goto L_0x0004;
    L_0x001f:
        if (r5 > r6) goto L_0x0003;
    L_0x0021:
        r0 = r3[r5];
        r1 = ˋ(r0);
        r1 = r1 << 10;
        r2 = ˊ(r0);
        r2 = r2 << 5;
        r1 = r1 | r2;
        r0 = ˏ(r0);
        r0 = r0 | r1;
        r3[r5] = r0;
        r5 = r5 + 1;
        goto L_0x001f;
        */
        throw new UnsupportedOperationException("Method not decompiled: o.ma.ˋ(int[], int, int, int):void");
    }

    private boolean ॱ(int i) {
        int ॱॱ = ॱॱ(i);
        fu.ॱ(ॱॱ, this.ʽ);
        return ˊ(ॱॱ, this.ʽ);
    }

    private boolean ˋ(b bVar) {
        return ˊ(bVar.ˏ(), bVar.ॱ());
    }

    private boolean ˊ(int i, float[] fArr) {
        if (this.ˏ == null || this.ˏ.length <= 0) {
            return false;
        }
        for (o.lz.e ˊ : this.ˏ) {
            if (!ˊ.ˊ(i, fArr)) {
                return true;
            }
        }
        return false;
    }

    private static int ʽ(int i) {
        return ((ˎ(Color.red(i), 8, 5) << 10) | (ˎ(Color.green(i), 8, 5) << 5)) | ˎ(Color.blue(i), 8, 5);
    }

    static int ˋ(int i, int i2, int i3) {
        return Color.rgb(ˎ(i, 5, 8), ˎ(i2, 5, 8), ˎ(i3, 5, 8));
    }

    private static int ॱॱ(int i) {
        return ˋ(ˏ(i), ˊ(i), ˋ(i));
    }

    static int ˏ(int i) {
        return (i >> 10) & 31;
    }

    static int ˊ(int i) {
        return (i >> 5) & 31;
    }

    static int ˋ(int i) {
        return i & 31;
    }

    private static int ˎ(int i, int i2, int i3) {
        int i4;
        if (i3 > i2) {
            i4 = i << (i3 - i2);
        } else {
            i4 = i >> (i2 - i3);
        }
        return i4 & ((1 << i3) - 1);
    }
}
